import { HOME_URL } from "@/config/config";
import router from "@/routers";
import { Router } from "vue-router";
import { RouterPath } from "../enums/routerPath";
let defaultHome: string = HOME_URL;
export function setHome(home: string) {
	defaultHome = home;
}
export function getHome(): string {
	return defaultHome;
}

/**
 * @description 递归查询当前路由所对应的路由
 * @param {Array} menuList 所有菜单列表
 * @param {String} path 当前访问地址
 * @return array
 */
export function filterCurrentRoute(menuList: Menu.MenuOptions[], path: string) {
	let result = {};
	for (let item of menuList) {
		if (item.path === path) return item;
		if (item.children) {
			const res = filterCurrentRoute(item.children, path);
			if (Object.keys(res).length) result = res;
		}
	}
	return result;
}

/**
 * 页面跳转，只支持path模式，好处是历史记录带url参数
 * @param path
 * @param param
 */
export function gotoPage(path: RouterPath, param?: any) {
	try {
		router.push({ path: path, query: param });
	} catch (e) {
		console.log(e);
	}
}
export function resolvePage(path: RouterPath, param: any) {
	// let pathArgs = [];
	// for (let key in param) {
	// 	pathArgs.push(`${key}=${param[key]}`);
	// }
	const url = router.resolve({ path: path, query: param });
	window.open(url.href);
}
export function openWin(path: string, router?: Router) {
	if (path.startsWith("http")) {
		window.open(path, "_blank");
	} else if (!router) {
		window.open(window.location.origin + path, "_blank");
	} else {
		const href = router.resolve({
			//使用resolve
			// name: path.replaceAll("/", "_"), //这里是跳转页面的name
			path: path
		});
		// 点击事件
		window.open(href.href, "_blank");
	}
}
